/*
 * @Author: shengduiliang 1107722390@qq.com
 * @Date: 2024-05-04 15:46:45
 * @LastEditors: shengduiliang 1107722390@qq.com
 * @LastEditTime: 2024-05-04 18:11:43
 * @FilePath: /treasure/src/network/upload.ts
 * @Description: 上传文件
 */
// 在小程序中引入腾讯云SDK
import COS from "cos-wx-sdk-v5";
import dayjs from "dayjs";

// 初始化腾讯云COS
const cos = new COS({
  SecretId: 'AKID68yhEey5BAA94793tifchpu795Z4eJhs',
  SecretKey: '52mNl19A5tdWguumdME5tskXDmBd4SmN'
});


// 上传文件
const uploadFile: (filePath: string) => Promise<string> = (filePath: string) => {

  const fileType = filePath.split('.').pop();
  const keyName = `/${dayjs().format('YYYYMMDD')}/${Math.random().toString(36).substring(2, 15)}.${fileType}`

  return new Promise((resolve, reject) => {
    cos.postObject(
      {
        Bucket: 'our-group-1257582885',
        Region: 'ap-guangzhou',
        Key: `/treasury${keyName}`,
        FilePath: filePath
      }, 
      (err, _) => {
        if (err) {
          reject(err);
        } else {
          resolve(keyName);
        }
      });
    }
  );
}

export {
  uploadFile
}